<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文件名称" prop="fileName">
        <el-input
            v-model="queryParams.fileName"
            placeholder="请输入文件名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件类型" prop="fileType">
        <el-select v-model="queryParams.fileType" placeholder="请选择文件类型" clearable>
          <el-option
              v-for="dict in dict.type.work_sign_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审批进度" prop="schedule">
        <el-select v-model="queryParams.schedule" placeholder="请选择审批进度" clearable>
          <el-option
              v-for="dict in dict.type.common_schedule"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['work:sign:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['work:sign:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['work:sign:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['work:sign:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="signList" @selection-change="handleSelectionChange">
      <el-table-column fixed type="selection" width="55" align="center"/>
      <el-table-column label="签署编号" align="center" prop="signId"/>
      <el-table-column label="文件名称" align="center" prop="fileName"/>
      <el-table-column label="文件份数" align="center" prop="fileShare"/>
      <el-table-column label="文件类型" align="center" prop="fileType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.work_sign_type" :value="scope.row.fileType"/>
        </template>
      </el-table-column>
      <el-table-column label="签署事由" align="center" prop="subject"/>
      <el-table-column label="审批进度" align="center" prop="schedule">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.common_schedule" :value="scope.row.schedule"/>
        </template>
      </el-table-column>
      <el-table-column label="签署日期" align="center" prop="signDate" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主文件" align="center" prop="masterFile">
        <template slot-scope="scope">
          <el-popover
              v-if="scope.row.masterFile && scope.row.masterFile.length > 0"
              placement="top"
              trigger="hover"
              width="400"
          >
            <el-row v-for="(item,index) in scope.row.masterFile" :key="index"
                    style="display: flex;align-items: center"
            >
              <el-col :span="20">
                <div class="grid-content">
                  {{ item.fileName }}
                </div>
              </el-col>
              <el-col :span="4">
                <div class="grid-content">
                  <el-button icon="el-icon-bottom" size="mini" type="text"
                             @click="downloadFile(item.fileUrl,item.fileName)">下载
                  </el-button>
                </div>
              </el-col>
            </el-row>
            <el-button slot="reference">主文件</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="附件" align="center" prop="files">
        <template slot-scope="scope">
          <el-popover
              v-if="scope.row.files && scope.row.files.length > 0"
              placement="top"
              trigger="hover"
              width="400"
          >
            <el-row v-for="(item,index) in scope.row.files" :key="index"
                    style="display: flex;align-items: center"
            >
              <el-col :span="20">
                <div class="grid-content">
                  {{ item.fileName }}
                </div>
              </el-col>
              <el-col :span="4">
                <div class="grid-content">
                  <el-button icon="el-icon-bottom" size="mini" type="text"
                             @click="downloadFile(item.fileUrl,item.fileName)">下载
                  </el-button>
                </div>
              </el-col>
            </el-row>
            <el-button slot="reference">附件</el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button
              v-hasPermi="['work:sign:view']"
              icon="el-icon-reading"
              size="mini"
              type="text"
              @click="preView(scope.row)"
          >查看
          </el-button>

          <el-button
              v-if="scope.row.schedule === 'unapproved'"
              v-hasPermi="['work:sign:submit']"
              icon="el-icon-position"
              size="mini"
              type="text"
              @click="submit(scope.row)"
          >签章提交
          </el-button>

          <el-button
              v-if="scope.row.schedule === 'running'"
              v-hasPermi="['work:sign:revoke']"
              icon="el-icon-bottom"
              size="mini"
              type="text"
              @click="revoke(scope.row)"
          >签章撤销
          </el-button>

          <el-button
              v-if="scope.row.schedule === 'terminated' || scope.row.schedule === 'canceled'"
              v-hasPermi="['work:sign:resubmit']"
              icon="el-icon-position"
              size="mini"
              type="text"
              @click="resubmit(scope.row)"
          >重新提交
          </el-button>

          <el-dropdown v-hasPermi="['work:sign:edit','work:sign:remove']"
                       v-if=" scope.row.schedule === 'unapproved'
                       || scope.row.schedule === 'terminated'
                       || scope.row.schedule === 'canceled'
                       || checkRole(['admin'])"
          >
              <span class="el-dropdown-link">
                更多<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-hasPermi="['work:sign:edit']">
                <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                >修改
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item v-hasPermi="['work:sign:remove']">
                <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                >删除
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <SelectFlow ref="selectFlow"></SelectFlow>
  </div>
</template>

<script>
import {delSign, getSign, listSign, reapply, revoke, submit} from "@/api/work/sign";
import {checkRole} from "@/utils/permission";

export default {
  name: "Sign",
  dicts: ['work_sign_type', 'common_schedule'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 签署表格数据
      signList: [],
      // 删除标志(0代表存在,1代表删除)时间范围
      daterangeCreateTime: [],
      // 删除标志(0代表存在,1代表删除)时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        deptId: null,
        userName: null,
        deptName: null,
        fileName: null,
        fileType: null,
        schedule: null,
        signDate: null,
        createTime: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkRole,
    downloadFile(filePath, fileName) {
      let fileSuffix = filePath.substring(filePath.lastIndexOf('.'))
      this.download('/common/download/resource', {
        resource: filePath
      }, `${fileName}${fileSuffix}`)
    },

    // 预览
    preView(row) {
      if (row.schedule === 'running' || row.schedule === 'completed' || row.schedule === 'terminated') {
        this.$tab.openPage("查看电子详情详情", "/work/sign/viewSign/index", {
          procInsId: row.processId,
          formType: '1',
          businessId: row.signId,
        })
      } else {
        this.$tab.openPage("查看电子详情详情", "/work/sign/viewSign/index", {
          formType: '1',
          businessId: row.signId,
          definitionId: row.definitionId,
        })
      }
    },
    // 提交审核
    submit(row) {
      this.$confirm('确定提交审核吗?', '温馨提示！', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const signId = row.signId
        submit(signId).then(response => {
          this.$modal.msgSuccess('提交成功')
          this.getList()
        })
      }).catch(() => {
      })
    },
    // 撤销提交
    revoke(row) {
      this.$confirm('确定撤销提交审核吗?', '温馨提示！', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const signId = row.signId
        revoke(signId).then(response => {
          this.$modal.msgSuccess('撤销成功')
          this.getList()
        })
      }).catch(() => {
      })
    },
    // 重新提交
    resubmit(row) {
      this.$confirm('确定重新提交审核吗?', '温馨提示！', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const signId = row.signId
        reapply(signId).then(response => {
          this.$modal.msgSuccess('重新提交成功')
          this.getList()
        })
      }).catch(() => {
      })
    },
    /** 查询签署列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      listSign(this.queryParams).then(response => {
        response.rows.forEach((item) => {
          item.masterFile = item.signFileList.filter(item => {
            return item.type === '1'
          })
          item.files = item.signFileList.filter(item => {
            return item.type === '2'
          })
        })
        this.signList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.signId)
      if (!this.checkRole(['admin'])) {
        for (let i = 0; i < selection.length; i++) {
          if (selection[i].schedule === 'running' || selection[i].schedule === 'completed') {
            this.single = true
            this.multiple = true
            break
          } else {
            this.single = selection.length !== 1
            this.multiple = !selection.length
          }
        }
      } else {
        this.single = selection.length !== 1
        this.multiple = !selection.length
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.selectFlow.getFlow(this.$flowMenu.signFlowMenu)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const signId = row.signId || this.ids
      getSign(signId).then(response => {
        let data = response.data
        this.$tab.openPage("修改隐患", "/work/sign/updateSign/index", {
          definitionId: data.definitionId,
          processName: data.processName,
          businessId: data.signId,
        })
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const signIds = row.signId || this.ids;
      this.$modal.confirm('是否确认删除签署编号为"' + signIds + '"的数据项？').then(function () {
        return delSign(signIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('work/sign/export', {
        ...this.queryParams
      }, `sign_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style lang="scss" scoped>
.el-dropdown-link {
  cursor: pointer;
  color: var(--current-color);
}

.el-icon-arrow-down {
  font-size: 12px;
}
</style>
